package com.example.pilipili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pilipili.common.R;
import com.example.pilipili.pojo.entity.Privileges;
import com.example.pilipili.dao.PrivilegesMapper;
import com.example.pilipili.pojo.entity.Tag;
import com.example.pilipili.service.PrivilegesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liyv
 * @since 2023-12-01
 */
@Service
public class PrivilegesServiceImpl extends ServiceImpl<PrivilegesMapper, Privileges> implements PrivilegesService {

    @Autowired
    PrivilegesMapper privilegesMapper;
    @Override
    public R<Privileges> add(Privileges entry) {
        // 查重
        QueryWrapper<Privileges> qw = new QueryWrapper<>();
        qw.eq("name",entry.getName()) ;
        if(privilegesMapper.exists(qw)) return R.failed("该权限已经存在") ;
        // save
        privilegesMapper.insert(entry) ;
        return R.ok(entry);

    }

    @Override
    public R<Privileges> update(Privileges entry) {
        Privileges p = privilegesMapper.selectById(entry.getId()) ;
        if (entry.getName() != null) p.setName(entry.getName());
        if (entry.getDescription() != null) p.setDescription(entry.getDescription());
        privilegesMapper.updateById(p) ;
        return R.ok(p);
    }


}
